/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;
using Core.Model.Data.Generics;

namespace Core.Parser
{
    public static class DecodeUtils
    {
        internal static int ParseInt ( string text )
        {
            //  Convenience method to parse an integer from possibly text
            int ret=0;

            if ( text != null )
            {
                if ( ( int.TryParse( text, out ret ) ) )
                    return ret;
                else throw new Exception( "Could not parse int from text" );

            }
            else throw new Exception( "Could not parse int from text" );
                       
        }

        internal static string EncodeBase64 ( string text )
        {
            try
            {
                byte [] encbuff = System.Text.Encoding.UTF8.GetBytes( text );
                return Convert.ToBase64String( encbuff );
            }
            catch ( Exception e )
            {
                throw new Exception( "Error in base64Encode: " + e.Message, e );
            }
        }


        internal static string DecodeBase64 ( string text )
        {
            try
            {
                byte [] decbuff = Convert.FromBase64String( text );
                //return System.Text.Encoding.UTF8.GetString( decbuff );
                return System.Text.Encoding.Default.GetString( decbuff );
            }
            catch ( Exception e )
            {
                throw new Exception( "Error in base64Decode: " + e.Message, e );
            }

        }

        public static void XmlDocBase64ToUTF8 ( XmlNode node )
        {
            XmlAttribute attribute;

            if ( (node.Attributes != null) &&( attribute = node.Attributes ["encoding"] ) != null )
            {
                if ( attribute.Value == "base64" )
                    node.InnerText = DecodeBase64( node.InnerText );
            }
            else
            {
                foreach ( XmlNode child in node.ChildNodes )
                {
                    XmlDocBase64ToUTF8( child);

                }
            }
            if ( node.Name == "event-type" )
                node.InnerText = Core.Model.Events.Event.GetEventName( DecodeUtils.ParseInt( node.InnerText ) );
        }

        internal static string ParseStringFromNode ( XmlElement node )
        {
            switch ( node.GetAttribute( "encoding" ) )
            {
                case ( "base64" ):
                    {
                        return DecodeBase64( node.InnerText );
                    }

                case ( "UTF8" ):
                    {
                        return node.InnerText;
                    }

                default:
                    throw new Exception( "Can't decode string" );


            }


        }

        internal static bool ParseBool ( string text )
        {
            bool b;
            if ( text != null )
            {
                if ( Boolean.TryParse( text, out b ) )
                    return b;

                else throw new Exception( "Can't parse bool from string" );
            }
            else throw new Exception( "Can't parse bool from string" );
        }


        internal static int ParseHex ( string p )
        {
            throw new Exception( "The method or operation is not implemented." );
        }
        
        internal static GenericColour LookupGenericColour ( String name )
        {
            return new GenericColour( LookupColourFromOctetString( name ) );
        }

        internal static Color LookupColourFromOctetString ( String colour )
        {
            return LookupColour(Convert.FromBase64String( colour ) );
           // return LookupColourFromString( colour );
        }

        internal static Color LookupColourFromString ( String colour )
        {
            return LookupColour ( (new ASCIIEncoding()).GetBytes(colour));
        }
                
        private static Color LookupColour ( byte[] components)
        {

            if ( components.Length >4 )
                throw new Exception( "oi" );
            if ( components.Length < 4 )
                //           return Color.HotPink;
                throw new Exception( "oi" );
           // Color colour;

            int r = 0;
            int g = 0;
            int b = 0;
            int a = 255;

            //if ( name.Equals( "white", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.White;
            //else if ( name.Equals( "gray", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Gray;
            //else if ( name.Equals( "darkgray", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkGray;
            //else if ( name.Equals( "lightgray", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.LightGray;
            //else if ( name.Equals( "black", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Black;
            //else if ( name.Equals( "red", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Red;
            //else if ( name.Equals( "darkred", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkRed;
            //else if ( name.Equals( "blue", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Blue;
            //else if ( name.Equals( "darkblue", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkBlue;
            //else if ( name.Equals( "green", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Green;
            //else if ( name.Equals( "darkgreen", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkGreen;
            //else if ( name.Equals( "yellow", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Yellow;
            //else if ( name.Equals( "darkyellow", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Yellow;
            //else if ( name.Equals( "cyan", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Cyan;
            //else if ( name.Equals( "darkcyan", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkCyan;
            //else if ( name.Equals( "magenta", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Magenta;
            //else if ( name.Equals( "darkmagenta", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.DarkMagenta;
            //else if ( name.Equals( "orange", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Orange;
            //else if ( name.Equals( "pink", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.Pink;
            //else if ( name.Equals( "transparent", StringComparison.OrdinalIgnoreCase ) )
            //    colour = Color.FromArgb( 0, 0, 0, 0 );
            //else if ( name.StartsWith( "#" ) && name.Length == 7 )
            //{
            //    throw new Exception( "Code me" );
            //    //r = DecodeUtils.ParseHex( name.substring( 1, 3 ), 16 );
            //    //g = DecodeUtils.ParseHex( name.substring( 3, 5 ), 16 );
            //    //b = DecodeUtils.ParseHex( name.substring( 5, 7 ), 16 );
            //    //colour = new Color( r, g, b );
            //}
            //else if ( name.StartsWith( "=" ) && name.Length == 10 )
            //{
            //    throw new Exception( "Code me" );
            //    //r = DecodeUtils.ParseHex( name.Substring( 2, 4 ));
            //    //g = DecodeUtils.ParseHex( name.substring( 5, 7 ));
            //    //b = DecodeUtils.ParseHex( name.substring( 8, 10 ));
            //    //a = 255 - Builder.parseInt( name.substring( 11, 13 ), 16 );
            //    //colour = new Color( r, g, b, a );
            //}
            //else if ( name.Length == 3 )
            //{
            //    r = ( int ) name [0];
            //    g = ( int ) name [1];
            //    b = ( int ) name [2];
            //    colour = Color.FromArgb( r, g, b );
            //}
            //else if ( name.Length == 4 )
            //{
            //    r = ( int ) name [0];
            //    g = ( int ) name [1];
            //    b = ( int ) name [2];
            //    a = 255 - ( int ) name [3];
            //    //a = ( int ) name [3];
            //    colour = Color.FromArgb( a, r, g, b );
            //}
          //  else
          //  {

                if ( components.Length > 0 )
                {
                    r  = components [0];// ( int ) components [0] + ( int ) components [1];

                    if ( components.Length > 1 )
                    {
                        g =components [1];//( int ) components [2] + ( int ) components [3];
                    }

                    if ( components.Length > 2 )
                    {
                        b = components [2];//( int ) components [4] + ( int ) components [5];
                    }

                    if ( components.Length > 3 )
                    {
                        a = 255 - components [3];//(( int ) name [3]);
                        //a = ( int ) name [3];
                    }
                    
                }
                return Color.FromArgb( a, r, g, b );
          //  }
           // else
           // {
                //throw new Exception( "oi" );
                //log.warning( "Couldn't parse a colour from string " + name );
                //colour = Color.White;
            //    colour = Color.FromArgb( a, r, g, b );
            //}

            //return colour;
        }
    }
}
